package com.jiang.jetpackdemo.fragment

import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.jiang.jetpackdemo.database.RoomHelper
import com.jiang.jetpackdemo.model.FavoritesModel
import com.jiang.jetpackdemo.model.VideoRecommendInfo
import com.jiang.jetpackdemo.utils.addAllAndChange
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.Exception

class FavoritesPicViewModel : ViewModel() {
    val favoritesModelList by lazy { MutableLiveData<MutableList<FavoritesModel>>() }
    val sizeOfFavoritesModel by lazy { MutableLiveData<Int>() }
    private lateinit var appContext: Context
    private val roomHelper: RoomHelper by lazy { RoomHelper(appContext) }

    fun initRoom(context: Context) {
        appContext = context.applicationContext
    }

    init {
        favoritesModelList.value = ArrayList()
        sizeOfFavoritesModel.value = 0
    }

    fun getModelList(): MutableList<FavoritesModel> = favoritesModelList.value!!

    fun getData(type: FavoritesPicFragment.Type, onFinish: () -> Unit) {
        viewModelScope.launch(Dispatchers.Main) {
            when (type) {
                FavoritesPicFragment.Type.PICTURE -> getPicData()
                FavoritesPicFragment.Type.VIDEO -> getVideoData()
                else -> getNewsData()
            }
            if (favoritesModelList.value?.size!! > 0) onFinish()
        }
    }

    private suspend fun getPicData() {
        withContext(Dispatchers.IO) {
            val queryAll = roomHelper.favoritesModelDao.queryAll()
                .filter { it.type == FavoritesModel.TYPE_PICTURE }
            if (queryAll.isNotEmpty()) {
                favoritesModelList.value?.apply {
                    clear()
                    addAllAndChange(queryAll, sizeOfFavoritesModel)
                }
            }
        }
    }

    fun cancelPic(pos: Int) {
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                roomHelper.favoritesModelDao.delete(favoritesModelList.value?.get(pos)!!)
                getPicData()
            }
        }


    }

    suspend fun getNewsData() {

    }

    private suspend fun getVideoData() {
        withContext(Dispatchers.IO) {
            val queryAll = roomHelper.favoritesModelDao.queryAll()
                .filter { it.type == FavoritesModel.TYPE_VIDEO }
            if (queryAll.isNotEmpty()) {
                favoritesModelList.value?.apply {
                    clear()
                    addAllAndChange(queryAll, sizeOfFavoritesModel)
                }
            }
        }
    }

    fun resultTran(pos: Int): String {
        try {
            val favoritesModel = favoritesModelList.value!![pos]
            val resultBean = VideoRecommendInfo.ResultBean()
            resultBean.data = VideoRecommendInfo.ResultBean.DataBean()
            resultBean.data.apply {
                title = favoritesModel.title
                playUrl = favoritesModel.contentUrl
                description = favoritesModel.des
                cover = VideoRecommendInfo.ResultBean.DataBean.CoverBean()
                cover.feed = favoritesModel.imgUrl
                id = favoritesModel.recommendId
            }
            val toJson = Gson().toJson(resultBean)
            return toJson
        } catch (e: Exception) {
            Log.e("resultTran", "resultTran: ${e.localizedMessage}", e)
            return ""
        }


    }

}